(function(obj) {
	var config = {
			accessKeyId: '', 
			secretAccessKey: '',
			sessionToken: '', 
			region:'cn-north-1',
			bucket:''
	};
	var s3;
	obj.init=function() {
		this.getS3Token();
		AWS.config.update(config);
		s3 = new AWS.S3({
		  apiVersion: '2006-03-01'
		});
		return this;
	};
	
	obj.getS3Token = function(){
		$.ajax({
			  type: 'POST',
			  url: Config.baseUrl+'/u/getS3Token',
			  traditional: true,
			  async: false,
			  beforeSend:Config.beforeSend,
			  success: function(data){
				  if(data.code == 1){
					  var awsInfo = data.obj;
				        for (var key in awsInfo) {
				            if (typeof awsInfo[key] !== 'undefined') {
				                config[key] = awsInfo[key];
				            }
				        }
				  }else{
					  obj.onerror(data);
				  }
			  }
		});
	};
	var reUpload = true;
	obj.upload = function(file,path,call){
		var key = path+file.name;
		var ContentType = file.type;
		 var params = {
				  Bucket: config.bucket, 
				  Body:file,
				  Key: key,
				  ContentType:ContentType
				 };
		 s3.upload(params, function(err, data) {
			 if(err && err.statusCode == 403 && reUpload){
				 reUpload = false;
				 obj.getS3Token();
				 obj.upload(file,path,call);
			 }else{
				 call(err,data);				 
			 }
		 });
	};
	obj.onerror = function(data) {
 
	}
 
})(S3Util = {});